﻿create function [Base64].[Encode] ( @p_String varchar(max) )
returns varchar(max) as 
begin

  declare @returnString varchar(max);
  
  select @returnString = Convert( Xml, '' ).value( 'xs:base64Binary(xs:hexBinary(sql:column("BinaryData")))' , 'varchar(max)' )
    from ( select Convert( varbinary(max), @p_String ) As BinaryData ) As tmp;

  return @returnString;

end